package com.example.childrendapp.gamefunny;

import com.example.childrendapp.R;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;


public class MyGameView extends View {
	private MyPicture[] pictures = new MyPicture[3]; // array that holds the
	// balls
	private int balID = 0; // variable to know what ball is being dragged
	private int touchX;
	private int touchY;
	private String TAG = "check rect";
	private MediaPlayer mediaPlayerDog, mediaPlayerChicken, mediaPlayerBird,
			mediaPlayerCows;
	private int olPicX1 = 50;
	private int olPicY1 = 20;
	private int olPicX2 = 100;
	private int olPicY2 = 20;
	private int olPicX3 = 150;
	private int olPicY3 = 20;

	public MyGameView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		Point point1 = new Point();
		point1.x = 50;
		point1.y = 20;
		Point point2 = new Point();
		point2.x = 100;
		point2.y = 20;
		Point point3 = new Point();
		point3.x = 150;
		point3.y = 20;

		// declare each ball with the ColorBall class
		pictures[0] = new MyPicture(context, R.drawable.bol_groen, point1);
		pictures[1] = new MyPicture(context, R.drawable.bol_rood, point2);
		pictures[2] = new MyPicture(context, R.drawable.bol_blauw, point3);

		initVariable();
	}

	public MyGameView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Point point1 = new Point();
		point1.x = 50;
		point1.y = 20;
		Point point2 = new Point();
		point2.x = 100;
		point2.y = 20;
		Point point3 = new Point();
		point3.x = 150;
		point3.y = 20;

		// declare each ball with the ColorBall class
		pictures[0] = new MyPicture(context, R.drawable.bol_groen, point1);
		pictures[1] = new MyPicture(context, R.drawable.bol_rood, point2);
		pictures[2] = new MyPicture(context, R.drawable.bol_blauw, point3);

		initVariable();
	}

	public MyGameView(Context context) {
		super(context);
		setFocusable(true); // necessary for getting the touch events
		// setting the start point for the balls
		Point point1 = new Point();
		point1.x = 50;
		point1.y = 20;
		Point point2 = new Point();
		point2.x = 100;
		point2.y = 20;
		Point point3 = new Point();
		point3.x = 150;
		point3.y = 20;

		// declare each ball with the ColorBall class
		pictures[0] = new MyPicture(context, R.drawable.bol_groen, point1);
		pictures[1] = new MyPicture(context, R.drawable.bol_rood, point2);
		pictures[2] = new MyPicture(context, R.drawable.bol_blauw, point3);

		initVariable();
	}

	// Variable
	public void initVariable() {
		mediaPlayerCows = MediaPlayer.create(getContext(), R.raw.bokeu);
		mediaPlayerBird = MediaPlayer.create(getContext(), R.raw.chimse);
		mediaPlayerChicken = MediaPlayer.create(getContext(), R.raw.gagay);
		mediaPlayerDog = MediaPlayer.create(getContext(), R.raw.chosua);
	}

	// the method that draws the balls
	@Override
	protected void onDraw(Canvas canvas) {
		// canvas.drawColor(0xFFCCCCCC); //if you want another background color
		drawRect(canvas);
		// draw the balls on the canvas
		for (MyPicture pic : pictures) {
			canvas.drawBitmap(pic.getBitmap(), pic.getX(), pic.getY(), null);
		}
		// draw hands
		// drawHand(canvas);

	}

	// draw 3 rect
	protected void drawRect(Canvas canvas) {
		Paint mpaint = new Paint();
		mpaint.setColor(Color.RED);
		mpaint.setAntiAlias(true);
		canvas.drawRect(130, 130, 190, 190, mpaint);
		canvas.drawRect(200, 200, 260, 260, mpaint);
		canvas.drawRect(300, 300, 360, 360, mpaint);
	}

	// --- runmusic with each picture
	public void runMusicWithEachPicture() {
		if (balID == 1) {
			mediaPlayerBird.start();
			if (mediaPlayerCows.isPlaying() == true) {
				mediaPlayerCows.pause();
			} else if (mediaPlayerChicken.isPlaying() == true) {
				mediaPlayerChicken.pause();
			}
		} else if (balID == 2) {
			mediaPlayerChicken.start();

			if (mediaPlayerBird.isPlaying() == true) {
				mediaPlayerBird.pause();
			}
			if (mediaPlayerCows.isPlaying() == true) {
				mediaPlayerCows.pause();
			}
		} else if (balID == 3) {
			mediaPlayerCows.start();

			if (mediaPlayerBird.isPlaying() == true) {
				mediaPlayerBird.pause();
			}
			if (mediaPlayerChicken.isPlaying() == true) {
				mediaPlayerChicken.pause();
			}
		}
	}

	// events when touching the screen
	public boolean onTouchEvent(MotionEvent event) {
		touchX = (int) event.getX();
		touchY = (int) event.getY();
		int eventaction = event.getAction();

		int X = (int) event.getX();
		int Y = (int) event.getY();

		switch (eventaction) {

		case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on
			// a ball
			// balID = 0;
			for (MyPicture pic : pictures) {
				// check if inside the bounds of the ball (circle)
				// get the center for the ball
				int centerX = pic.getX() + 25;
				int centerY = pic.getY() + 25;

				// calculate the radius from the touch to the center of the ball
				double radCircle = Math
						.sqrt((double) (((centerX - X) * (centerX - X)) + (centerY - Y)
								* (centerY - Y)));

				// if the radius is smaller then 23 (radius of a ball is 22),
				// then it must be on the ball
				if (radCircle < 23) {
					balID = pic.getID();
					runMusicWithEachPicture();
					break;
				} else {
					// check when ball on area we want. when touch again, this
					// touch not move other ball.
					// so we give balID = 0;
					balID = 0;
				}

				// check all the bounds of the ball (square)
				// if (X > ball.getX() && X < ball.getX()+50 && Y > ball.getY()
				// && Y < ball.getY()+50){
				// balID = ball.getID();
				// break;
				// }
			}

			break;

		case MotionEvent.ACTION_MOVE: // touch drag with the ball
			// move the balls the same as the finger
			if (balID > 0) {
				pictures[balID - 1].setX(X - 25);
				pictures[balID - 1].setY(Y - 25);
			}

			break;

		case MotionEvent.ACTION_UP:
			// draw rectbound 1 - and set picture 1 is true
			Rect mRect1 = new Rect(130, 130, 190, 190);
			/*
			 * if ((mRect1.contains(touchX, touchY)) && balID == 1) {
			 * pictures[0].setX(130); pictures[0].setY(130); Log.i(TAG,
			 * "Ball 1 True"); }
			 */
			if (mRect1.contains(touchX, touchY)) {
				if (balID == 1) {
					pictures[0].setX(130);
					pictures[0].setY(130);
					Log.i("Check Rect", "True");
				} else {
					if(balID == 2){
						pictures[1].setX(olPicX2);
						pictures[1].setY(olPicY2);
					}
					if(balID ==  3){
						pictures[2].setX(olPicX3);
						pictures[2].setY(olPicY3);
					}
					Log.i("Check Rect", "Faile");
				}

			}

			// create rect 2 - and set picture 2 is true
			Rect mRect2 = new Rect(200, 200, 260, 260);
			if ((mRect2.contains(touchX, touchY)) && balID == 2) {
				Log.i(TAG, "Ball 2 True");
				pictures[1].setX(200);
				pictures[1].setY(200);
			}
			// create rect3 - and set it has picture 3
			Rect mRect3 = new Rect(300, 300, 360, 360);

			if ((mRect3.contains(touchX, touchY)) && balID == 3) {
				Log.i(TAG, "Ball 3 True");
				pictures[2].setX(300);
				pictures[2].setY(300);
			} else {

			}

			break;
		}
		// redraw the canvas
		invalidate();
		return true;

	}
}
